<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mod_check extends CI_Controller {    
    function  __construct(){
        parent::__construct();
        if(!($this->session->userdata('logedin')&&$this->session->userdata('admin')==1)){
            redirect('ad_user');            
            exit();
        }
    }
    function index(){
        echo 'Làm gì thế?';
    }
    function checkip(){        
        $data->content=$this->load->view('mod_check/ip',array(),true); 
        $dt = $this->template->render($data);  
        $this->load->view('default/viewfull',$dt);
    }
    function checkpoint(){ 
        $user='';
        $mcre='';
        if(!empty($_POST)){            
            $this->session->set_userdata('iploc',$this->input->post('ip',true));
        }
        $iploc = $this->session->userdata('iploc'); 
        if($iploc){
            // $ip = explode('\n', $dulieu);
           $ip2 = explode("\n", str_replace("\r", "",$iploc ));           
           if(!empty($ip2)){
                $mm= array();
                $username=array_map('trim',$ip2);//mag username
              //  array_unique()
                $user = $this->db->where_in('username',$username)->get('users')->result();
                $iduser = '';
                foreach ($user as $user1){
                    $iduser[]= $user1->id;
                }
                //print_r($iduser);
                $credit =$this->db->where_in('iduser',$iduser)->get('credit')->result();
                if(!empty($credit)){
                    foreach ($credit as $credit1){
                        $mcre[$credit1->iduser][]=$credit1;
                    }
                }
                     
            }
        }
        $data->content =  $this->load->view('mod_check/checkpoint', array('user'=>$user,'credit'=>$mcre),true);
        $dt = $this->template->render($data);  
        $this->load->view('default/viewfull',$dt);
    }
    function click($iduser=0){
        $this->db->where('id_user',(int)$iduser);
        $this->db->order_by('id','DESC');
        $this->db->select(
                            'tracklink.*,                            
                            offer.title, 
                            COUNT(*) as sl
                            ');
        $this->db->from('tracklink');
        $this->db->join('offer', 'offer.id = tracklink.id_offer','left');
        $this->db->group_by('id_offer');   
        $query = $this->db->get();        
        $dt = $query->result();
        $data->content =  $this->load->view('mod_check/click', array('dt'=>$dt),true);
        $dt = $this->template->render($data);  
        $this->load->view('default/viewfull',$dt);
    }
    function delcredit($id = 0){
        $this->db->where('id',(int)$id);
        $this->db->delete('credit');
        redirect(base_url().'mod_check/checkpoint');
    }
    function edituser($id = 0){
        if($this->input->post()){
            
            $this->db->where('id',(int)$id);
            $this->db->update('users',
            array(
                'total'=>$this->input->post('total'),
                'curent'=>$this->input->post('curent'),
                'pending'=>$this->input->post('pending')
            )
            );
            redirect(base_url().'mod_check/checkpoint');
        }
        $user = $this->Home_model->get_one('users',array('id'=>(int)$id));        
        $data->content =  $this->load->view('mod_check/useredit', array('dulieu'=>$user),true);
        $dt = $this->template->render($data);  
        $this->load->view('default/viewfull',$dt);
    }
    function ajaxcheckip(){
        if(!empty($_POST)){
           // $ip = explode('\n', $dulieu);
           $ip2 = explode("\n", str_replace("\r", "", $this->input->post('ip',true)));           
           if(!empty($ip2)){
                $mm= array();
                $ip2=array_map('trim',$ip2);
                $this->db->select('ip');
                $this->db->where_in('ip',$ip2);                
                $mangip = $this->db->get('users')->result_array();   
                foreach ($mangip as $mangip){
                    $mm[]=trim($mangip['ip']);
                }                         
                $mm= array_unique ($mm);//loai bo phan tu trung nhau
                $cothesudung = array_diff ($ip2, $mm);//tra ve mang gom cac phan tu co trong $ip2 ma k co trong $mangip
                $khongthe=array_intersect ($ip2, $mm); //tra ve cac ip da ton tai  
                foreach ($cothesudung as $cothesudung){
                    echo $cothesudung.'<br>';
                }  
                foreach ($khongthe as $khongthe){
                    echo '<span>'.$khongthe.'</span><br>';
                }
                     
            }   
        }
    }
 
}